
namespace Core {
    export class BaseScene {
        private _layers: Array<Laya.Sprite>;

        public constructor() {
            this._layers = new Array<Laya.Sprite>();
        }

        public onEnter(): void {
        }

        public onExit(): void {
            App.ViewManager.closeAll();
            this.removeAllLayer();
        }

        public addLayer(layer: Laya.Sprite): void {
            if (layer instanceof BaseSpriteLayer) {
                App.StageUtils.getStage().addChild(layer);
                this._layers.push(layer);
            }
            else if (layer instanceof BaseEuiLayer) {
                App.StageUtils.getUIStage().addChild(layer);
                this._layers.push(layer);
            }
        }

        public removeLayer(layer: Laya.Sprite): void {
            if (layer instanceof BaseSpriteLayer) {
                App.StageUtils.getStage().removeChild(layer);
                this._layers.splice(this._layers.indexOf(layer), 1);
            }
            else if (layer instanceof BaseEuiLayer) {
                App.StageUtils.getUIStage().removeChild(layer);
                this._layers.splice(this._layers.indexOf(layer), 1);
            }
        }

        public removeAllLayer(): void {
            while (this._layers.length) {
                var layer: Laya.Sprite = this._layers[0];
                this.layerRemoveAllChild(layer);
                this.removeLayer(layer);
            }
        }

        public layerRemoveAllChild(layer: Laya.Sprite): void {
            if (layer instanceof BaseSpriteLayer) {
                (<BaseSpriteLayer>layer).removeChildren();
            }
            else if (layer instanceof BaseEuiLayer) {
                (<BaseEuiLayer>layer).removeChildren();
            }
        }
    }
}